﻿using ACC.Business;
using ACC.Business.Stn;
using ACC.Data;
using ACC.Data.ACCLog;
using ACC.Data.Comm;
using ACC.Data.Entities.Common;
using ACC.Data.Entities.Mes;
using ACC.Data.Line;
using ACC.Device;
using ACC.LineDevice.KukaPack;
using iPlant.Features.WOMSClient.Entity;
using System;
using System.Collections.Generic;
using System.Threading;

namespace Script
{
    /// <summary>
    /// 充放电测试
    /// OP L1P08030 STATION L1P08031-1 ... L1P08031-4
    ///                     L1P08032-1 ... L1P08032-4
    ///                     L1P08033-1 ... L1P08033-4
    ///                     L1P08034-1 ... L1P08034-4
    ///                     L1P08035-1 ... L1P08035-4
    ///                     L1P08036-1 ... L1P08036-4
    ///                     L1P08037-1 ... L1P08037-4
    /// </summary>
    class L1P08030 : StnProcessKukaPack
    {
        public L1P08030(StnRunningData data)
            : base(data)
        {
        }

        protected override ACCDevice CreateDevice()
        {
            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                #region 设置PLC起始地址
                switch (_data.StnCfg.Name)
                {
                    case "L1P08031-1":
                        kukaSiemensDev.DB190_Offset = 8000;
                        break;
                    case "L1P08031-2":
                        kukaSiemensDev.DB190_Offset = 9000;
                        break;
                    case "L1P08031-3":
                        kukaSiemensDev.DB190_Offset = 10000;
                        break;
                    case "L1P08031-4":
                        kukaSiemensDev.DB190_Offset = 11000;
                        break;

                    case "L1P08032-1":
                        kukaSiemensDev.DB190_Offset = 12000;
                        break;
                    case "L1P08032-2":
                        kukaSiemensDev.DB190_Offset = 13000;
                        break;
                    case "L1P08032-3":
                        kukaSiemensDev.DB190_Offset = 14000;
                        break;
                    case "L1P08032-4":
                        kukaSiemensDev.DB190_Offset = 15000;
                        break;

                    case "L1P08033-1":
                        kukaSiemensDev.DB190_Offset = 16000;
                        break;
                    case "L1P08033-2":
                        kukaSiemensDev.DB190_Offset = 17000;
                        break;
                    case "L1P08033-3":
                        kukaSiemensDev.DB190_Offset = 18000;
                        break;
                    case "L1P08033-4":
                        kukaSiemensDev.DB190_Offset = 19000;
                        break;

                    case "L1P08034-1":
                        kukaSiemensDev.DB190_Offset = 20000;
                        break;
                    case "L1P08034-2":
                        kukaSiemensDev.DB190_Offset = 21000;
                        break;
                    case "L1P08034-3":
                        kukaSiemensDev.DB190_Offset = 22000;
                        break;
                    case "L1P08034-4":
                        kukaSiemensDev.DB190_Offset = 23000;
                        break;

                    case "L1P08035-1":
                        kukaSiemensDev.DB190_Offset = 24000;
                        break;
                    case "L1P08035-2":
                        kukaSiemensDev.DB190_Offset = 25000;
                        break;
                    case "L1P08035-3":
                        kukaSiemensDev.DB190_Offset = 26000;
                        break;
                    case "L1P08035-4":
                        kukaSiemensDev.DB190_Offset = 27000;
                        break;

                    case "L1P08036-1":
                        kukaSiemensDev.DB190_Offset = 28000;
                        break;
                    case "L1P08036-2":
                        kukaSiemensDev.DB190_Offset = 29000;
                        break;
                    case "L1P08036-3":
                        kukaSiemensDev.DB190_Offset = 30000;
                        break;
                    case "L1P08036-4":
                        kukaSiemensDev.DB190_Offset = 31000;
                        break;

                    case "L1P08037-1":
                        kukaSiemensDev.DB190_Offset = 32000;
                        break;
                    case "L1P08037-2":
                        kukaSiemensDev.DB190_Offset = 33000;
                        break;
                    case "L1P08037-3":
                        kukaSiemensDev.DB190_Offset = 34000;
                        break;
                    case "L1P08037-4":
                        kukaSiemensDev.DB190_Offset = 35000;
                        break;
                }
                #endregion

                kukaSiemensDev.DB190 = 800;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB192_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 66);
                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB193_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 34);
                kukaSiemensDev.DB193_ACC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 68);
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB194_PLC_Offset = (ushort)(kukaSiemensDev.DB190_Offset + 34);
                kukaSiemensDev.DB194_PLC_Data_Offset = kukaSiemensDev.DB190_Offset + 78;
            }

            return dev;

        }

        public override ACCCode ReadLoadParam(ACCDevice dev, out ACCDevice.LoadParam param)
        {
            IsReport = true;  // 使用底层的进站报工处理  
            return base.ReadLoadParam(dev, out param);
        }

        public override ACCCode SendUnloadResult(ACCDevice dev, ACCDevice.UnloadResult result)
        {
            string msg = "";
            IsReport = false;   //不使用底层的出站报工处理 
            ACCCode code = base.SendUnloadResult(dev, result);
            if (code == ACCCode.ACCOK && result.ErrorCode == ACCCode.ACCOK)
            {
                var v = _data.LastUnloadUnit.LastHist.Result == WIPUnitHist.UnitHistoryResult.NOK ? UnitStatus.NG :
                    (_data.LastUnloadUnit.Status == WIPUnit.UnitStatus.FG ? UnitStatus.FG : UnitStatus.NG);
                _womsProcess.ChargeWorkOrder(_data.LastUnloadUnit.UnitSN, "", "", v, out msg);

                try
                {
                    var param = new Dictionary<string, string>();
                    // 充放电测试工位过程数据采集
                    param.Clear();
                    param.Add("line", _lineData.Line);
                    param.Add("stn", _data.StnCfg.Name);
                    param.Add("barcode", GetGBSN(_data.LastUnloadUnit.UnitSN, _lineData.Line, "GBSN"));
                    param.Add("unitsn", _data.LastUnloadUnit.UnitSN);
                    new Thread(new ThreadStart(delegate()
                    {
                        var ret = PostWebApiMessage("UploadChargeDischargeData", "", param);
                        if (ret != null && !ret.IsSuccess)
                            logger.Debug("上传充放电测试数据失败,Error=" + ret.Msg);

                    })).Start();
                }
                catch (Exception ex)
                {
                    logger.Error(ex.StackTrace);
                }
                StnEventLog.Log(_data, StnLog.LogType.Error,
                        string.Format("出站报工{0} UnitSN={1}, ErrorMessage={2}", "失败", _data.LastUnloadUnit.UnitSN, msg));

            }

            return code;
        }
    }
}
